<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>公众号管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
      </el-form-item>
      <el-form-item label="" v-for="(filter, index) in formInline.filters" :key="index">
        <el-input size="small" v-model="filter.fieldValue" v-bind:fieldName="filter.fieldName"
                  v-bind:fieldValue="filter.fieldValue" v-bind:compare="filter.compare"
                  @keyup.enter.native="search"
                  :placeholder="filter.placeholder" clearable/>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleAdd()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-empty :image-size="200" v-if="!listData.length"></el-empty>
    <el-table size="small" v-if="listData.length" :data="listData" highlight-current-row v-loading="table.loading"
              border
              element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column prop="name" label="公众号名称">
      </el-table-column>
      <el-table-column prop="appid" label="APPID">
      </el-table-column>
      <el-table-column prop="wxAccount" label="微信号">
      </el-table-column>
      <el-table-column prop="atype.desc" label="公众号类型">
      </el-table-column>
      <el-table-column prop="authentication.desc" label="是否认证">
      </el-table-column>
      <el-table-column prop="tokenTime" label="上次获取TOKEN时间" width="250">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.tokenTime }}</span>
          <el-button type="success" icon="el-icon-refresh" circle @click="refreshToken(scope.row)"></el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteVar(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!--    编辑-->
    <el-dialog :title="formInfo.title" :visible.sync="formInfo.visiable" @click="closeDialog">
      <el-form label-width="120px" :model="formInfo.formData" :rules="rules" ref="formInfo.formData">

        <el-row>
          <el-col :span="12">
            <el-form-item label="公众号名称" prop="name">
              <el-input size="small" v-model="formInfo.formData.name" auto-complete="off" placeholder="公众号名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="原始ID" prop="originId">
              <el-input size="small" v-model="formInfo.formData.originId" auto-complete="off"
                        placeholder="原始ID"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="appid" prop="appid">
              <el-input size="small" v-model="formInfo.formData.appid" auto-complete="off"
                        placeholder="原始ID"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公众号类型" prop="atype">
              <el-select v-model="formInfo.formData.atype" placeholder="请选择公众号类型">
                <el-option :label="item.name" :value="item.value"
                           v-for="(item, index) in [{name: '服务号', value: 0}, {name: '订阅号', value: 1}]"
                           :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="是否认证" prop="authentication">
              <el-select v-model="formInfo.formData.authentication" placeholder="请选择是否认证">
                <el-option :label="item.name" :value="item.value"
                           v-for="(item, index) in [{name: '否', value: 0}, {name: '是', value: 1}]"
                           :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="微信号" prop="wxAccount">
              <el-input size="small" v-model="formInfo.formData.wxAccount" auto-complete="off"
                        placeholder="微信号"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="AppSecret" prop="appSecret">
              <el-input size="small" v-model="formInfo.formData.appSecret" auto-complete="off"
                        placeholder="AppSecret"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="formInfo.loading" class="title"
                   @click="submitForm('editForm')">保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import Pagination from "../../../components/Pagination";
  import {wxAccountEdit, wxAccountAdd, wxAccountDel, wxAccountList, wxRefreshToken} from '../../../api/baseMG'

  export default {
    data() {
      return {
        table: {
          loading: false
        },
        formInline: {
          page: 1,
          perPage: 10,
          filters: [
            {placeholder: "请输入公众号名称", fieldName: "name", compare: "like", fieldValue: ""}
          ],
        },
        rules: {
          name: [
            {required: true, message: '请输入公众号名称', trigger: 'blur'}
          ],
          originId: [
            {required: true, message: '请输入原始ID', trigger: 'blur'}
          ],
          appid: [
            {required: true, message: '请输入appid', trigger: 'blur'}
          ],
          atype: [
            {required: true, message: '请选择公众号类型', trigger: 'blur'}
          ],
          authentication: [
            {required: true, message: '请选择是否认证', trigger: 'blur'}
          ],
          wxAccount: [
            {required: true, message: '请输入微信号', trigger: 'blur'}
          ],
          appSecret: [
            {required: true, message: '请输入AppSecret', trigger: 'blur'}
          ]
        },
        formInfo: {
          title: "",
          visiable: false,
          loading: false,
          formData: {}
        },
        listData: []
      }
    },
    created() {
      this.search();
    },
    // 注册组件
    components: {
      Pagination
    },
    methods: {
      refreshToken(row) {
        this.$confirm('确定要刷新Token吗?', '信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          wxRefreshToken(row.id).then(res => {
            if(res.success) {
              this.search();
              this.$message({
                type: 'success',
                message: res.msg
              });
            }
          })
        });
      },
      search() {
        wxAccountList(this.formInline).then(res => {
          if (res.success) {
            this.listData = res.data.list;
            this.formInline.currentPage = res.data.pageNum;
            this.formInline.pageSize = res.data.pageSize;
            this.formInline.total = res.data.total;
          }
        });
      },
      handleAdd() {
        this.formInfo.title = "新增";
        this.formInfo.formData = {};
        this.formInfo.visiable = true;
      },
      handleEdit(index, row) {
        this.formInfo.title = "修改";
        this.formInfo.formData = JSON.parse(JSON.stringify(row));
        this.formInfo.formData.atype = row.atype.value;
        this.formInfo.formData.authentication = row.authentication.value;
        this.formInfo.visiable = true;
      },
      deleteVar(index, row) {
        this.$confirm('确定要删除吗?', '信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          wxAccountDel(row.id).then(res => {
            if (res.success) {
              this.$message({
                type: 'success',
                message: res.msg
              });
              this.search();
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        })
      },
      closeDialog() {
        this.formInfo.visiable = false;
      },
      submitForm() {
        this.$refs['formInfo.formData'].validate(valid => {
          if (valid) {
            console.log(this.formInfo.formData);
            if(this.formInfo.title == '修改') {
              wxAccountEdit(this.formInfo.formData).then(res => {
                if (res.success) {
                  this.formInfo.visiable = false;
                  this.$message({
                    type: 'success',
                    message: res.msg
                  });
                  this.search();
                } else {
                  this.$message({
                    type: 'error',
                    message: res.msg
                  })
                }
              });
            } else {
              wxAccountAdd(this.formInfo.formData).then(res => {
                if (res.success) {
                  this.formInfo.visiable = false;
                  this.$message({
                    type: 'success',
                    message: res.msg
                  });
                  this.search();
                } else {
                  this.$message({
                    type: 'error',
                    message: res.msg
                  })
                }
              });
            }
          }
        });

      }
    }
  }
</script>

<style scoped>
  .user-search {
    margin-top: 20px;
  }

  .userRole {
    width: 100%;
  }
</style>
